from django.shortcuts import render
from django.http import HttpResponseRedirect
# Create your views here.
from django.contrib.auth import authenticate
from django.http import JsonResponse
from django.http import HttpResponse
from django.urls import reverse
from learn.models import img
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth import logout
from django.shortcuts import render_to_response
from learn.project.daili import daili_ip
from django.contrib.auth.decorators import login_required
# admin 默认密码 a1625123170
import json
# 默认页

def old_login(request):
    return HttpResponseRedirect(reverse('new_index'))

# 登录页
def new_login(request):
    return render(request, 'login.html')

# 注册页
def register(request):
    return render(request,'register.html')

# 登录页检测
def login_check(request):
    if request.method == 'POST':
        seller_name = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=seller_name, password=password)
        if user is not None:
            login(request, user)
            response_data={'state': 'success', 'message': '登录成功', 'url': '/index'}
        else:
            message = '用户名或密码错误'
            response_data = {'state': 'fail', 'message': message}
    else:
        message = '无'
        response_data = {'state': 'fail', 'message': message}
    return JsonResponse(response_data)

# 注册检查
def register_check(request):
    print(1)
    if request.method == 'POST':
        seller_name = request.POST.get('username')
        password = request.POST.get('password')
        aa = User.objects.filter(username=seller_name)
        if len(aa) == 0:
            user = User.objects.create_user(username=seller_name, password=password)
            user.save
            login(request, user)
            response_data={'state': 'success', 'message': '注册成功', 'url': '/index'}
        else:
            message = '用户名已存在'
            response_data = {'state': 'fail', 'message': message}
    else:
        message = '无'
        response_data = {'state': 'fail', 'message': message}
    return JsonResponse(response_data)

# 退出登录
def loginout(request):
    logout(request)
    return HttpResponseRedirect(reverse('new_index'))

# 主页
def index(request):
    aa = img.objects.get(title='首页').headImg
    bb = str(aa)[13:]
    return render(request, 'index.html',{'images':bb})

# 代理ip首页
def agentip_index(request):
    aa = img.objects.get(title='首页').headImg
    bb = str(aa)[13:]
    return render(request, 'agentip.html',{'images':bb})

def agentip_test(request):
    if request.method == 'GET':
        dd = request.GET.get('Radio')
        if dd == '001':
            result_json = daili_ip.ip_Crawler().ip_start()
        elif dd == '002':
            result_json = daili_ip.pickle_operation().pickle_read('Lately', Interface=1)
        elif dd == '003':
            result_json = daili_ip.pickle_operation().pickle_read('Ally')
        elif dd == '004':
            date = request.GET.get('Date')
            result_json = daili_ip.pickle_operation().pickle_read('Date', date=str(date))
        result = str(json.loads(result_json))
        print(result)
        response_data = {'state': 'success', 'message': result}
    return JsonResponse(response_data)

@login_required(login_url='/index')
def Personal_Center_index(request):
    return render(request, 'Personal_Center.html')